namespace NBody.Analysis
{
    using System;
    
    public class Integration
    {
        public static double Trapezoidal(double[] x, double[] f, int a, int b)
        {
            double Tn = 0.0;
            
            if (a == b)
            {
                if (a == 0)
                    return f[a] * (x[a + 1] - x[a]);
                else
                    return f[a] * (x[a] - x[a - 1]);
            }

            for (int i = a; i < b; ++i)
            {
                Tn += 0.5 * (x[i + 1] - x[i]) * (f[i + 1] + f[i]);
            }

            return Tn;
        }
    }
}
